﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HarpoonMan.PlayerObjects;
using HarpoonMan.WorldObjects;
using FlatRedBall;
using FlatRedBall.Input;
using Microsoft.Xna.Framework;
using System.Diagnostics;

namespace HarpoonMan.Utilities
{


    public static class GameManager
    {
        private static List<Obstacle> _obstacles;
        private static List<Trigger> _triggerables;

        private static Player _player;
        private static Sprite _bg;
        private static Sprite _crosshair;

        public static Vector3 ReticlePos
        {
            get { return _crosshair.Position; }
        }

        public static float GRAVITY = -25.0f;

        public static void Initialize()
        {


        }

        public static void LoadLevel()
        {
            _obstacles = new List<Obstacle>();
            _triggerables = new List<Trigger>();
            _player = new Player("Global");

            _crosshair = SpriteManager.AddSprite(@"Content/UI/Crosshair", "Global");
            _crosshair.PixelScale();
            _crosshair.Z = .1f;
            _crosshair.X = InputManager.Mouse.WorldXAt(0.0f);
            _crosshair.Y = InputManager.Mouse.WorldYAt(0.0f);
            InputManager.Mouse.GrabbedPositionedObject = _crosshair;

            _bg = SpriteManager.AddSprite(@"Content/Scenery/Level1", "Global");
            _bg.PixelScale();
            _bg.Z = -.1f;


            Platform p = new Platform("Global");
            p.Y = -10.0f;

            Platform p2 = new Platform("Global");
            p2.X = 22.0f;
            p2.Y = -1.5f;
            

            Platform p3 = new Platform("Global");
            p3.Y = -15.0f;
            p3.X = 23.0f;

            _obstacles.Add(p);
            _obstacles.Add(p2);
            _obstacles.Add(p3);

            Door d = new Door("Global");
            d.X = 15.0f;
            d.Y = -5.0f;

            _obstacles.Add(d);

            Trigger t = new Trigger("Global", d.Open);
            t.X = 25.0f;
            t.Y = 10.0f;
            _triggerables.Add(t);

            Box b = new Box("Global");
            b.X = 10.0f;
            b.Y = 30.0f;
            _obstacles.Add(b);
        }

        public static void DestroyLevel()
        {
            _player.Destroy();
            _triggerables.Destroy();
            _obstacles.Destroy();
        }

        public static void Activity()
        {
            _player.Activity();
            CheckPlatformCollision();

            if (_player.Harpoon.State == HarpoonState.Throwing)
            {
                CheckHarpoonCollision();
            }

            InputManager.Mouse.ShowNativeWindowsCursor();

        }

        private static void CheckHarpoonCollision()
        {
            foreach (Obstacle o in _obstacles)
            {
                //Debug.WriteLine(o.GetType().ToString());
                if (o.GetType().Equals(typeof(Box))
                    && _player.Harpoon.Collision.CollideAgainstMove(o.Collision, 0.0f, 1.0f))
                {
                    o.AttachTo(_player.Harpoon, true);
                    _player.Harpoon.AttachedBox = (Box)o;
                    _player.Harpoon.State = HarpoonState.Grabbed;
                    return;
                }
                else if (o.GetType().Equals(typeof(Platform))
                    && _player.Harpoon.Collision.CollideAgainstMove(o.Collision, 0.0f, 1.0f))
                {
                    _player.Harpoon.State = HarpoonState.Crashed;
                    return;
                }
            }

            foreach (Trigger s in _triggerables)
            {
                if (_player.Harpoon.Collision.CollideAgainst(s.Collision) || _player.Collision.CollideAgainst(s.Collision))
                {
                    s.OnTrigger();
                    return;
                }
            }
        }

        private static void CheckPlatformCollision()
        {
            foreach (Obstacle p in _obstacles)
            {
                //if (p.GetType().Equals(typeof(Platform)) || p.GetType().Equals(typeof(Door)))
                //{
                    if ((p.GetType().Equals(typeof(Door))))
                    {
                        if (!((Door)p).Passable
                         && _player.Collision.CollideAgainstMove(p.Collision, 0.0f, 1.0f))
                        {
                            _player._sprite.Animate = true;
                        }
                    }
                    else if (p.GetType().Equals(typeof(Platform)))
                    {
                        if (_player.Collision.CollideAgainstMove(p.Collision, 0.0f, 1.0f)
                        && _player.Collision.LastMoveCollisionReposition.Y > 0)
                        {
                            _player.Acceleration.Y = 0;
                            _player.Jump = JumpState.Idle;
                            _player._sprite.Animate = true;
                        }

                        foreach (Obstacle b in _obstacles)
                        {
                            if (b.GetType().Equals(typeof(Box)))
                            {
                                if (b.Collision.CollideAgainstMove(p.Collision, 0.0f, 1.0f))
                                {
                                    if (b.Collision.LastMoveCollisionReposition.Y > 0)
                                    {
                                        b.Acceleration.Y = 0;
                                        b.Velocity.Y = 0;
                                    }
                                }
                                else
                                {
                                    b.Acceleration.Y = GRAVITY;
                                }
                            }
                        }
                    }
                    else if (p.GetType().Equals(typeof(Box)))
                    {
                        if (_player.Collision.CollideAgainstMove(p.Collision, 0.0f, 1.0f)
                        && _player.Collision.LastMoveCollisionReposition.Y > 0)
                        {
                            _player.Acceleration.Y = 0;
                            _player.Jump = JumpState.Idle;
                            _player._sprite.Animate = true;
                        }
                    }
                    else
                    {
                        _player.Acceleration.Y = GRAVITY;
                    }
                //}
            }
        }

    }
}
